<?php

//require_once (APPLICATION_PATH . "\\functions\\utilities.php");

class ProfileController extends Zend_Controller_Action
{
	
    public function init()
    {
       try{
    		$session = Zend_Registry::get('session');
    		
    		if($session->user['Username'] != ""){ //ako ne e prazna sesijata znaci deka korisnikot e logiran.
    			$viewuser = $this->_request->getQuery('id');
    			$this->view->viewuser = $viewuser;
    		}        
        	else{        		
    			$this->_helper->redirector('index','login'); //vo sprotivno vrati go na stranata za logiranje.
    		} 
        
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";  
        
        } 	
    			
    }

    public function indexAction()
    {
    	try{
    			$session = Zend_Registry::get('session');
    			
    			
				//TODO Permisii	   
    			//mora da ima id vo query za da se vidi profilot, ako nema pravi redirekt na home, 
    			if ($this->_request->getQuery('id') == ""){
    				$this->_helper->redirector('index','home');
    			}
    			
    			 		
	    		$status = new Application_Model_DbTable_Status();
    			
    			if ($session->user['id'] == $this->_request->getQuery('id')){
		      
		        	
		        	if ($this->getRequest()->isPost()) {
	        		
						if (isset($_POST['btn_StatusSubmit'])) {
							
							//TODO validcaija
							$text = $this->getRequest()->getParam("txt_Status");
							$user_id=$session->user['id'];				
							$time = new Zend_Date(); // zima datum i so site parametri
							$time=$time->toString('YYYY-MM-dd HH:mm:ss');
							//$dateUpdated=$time->toString("HH:mm:ss dd-MM-YYYY"); 	// se pravi kompatibilen format za vo MySql				
	
							$mood = $this->getRequest()->getParam("hdn_StatusValue");
							
							$status->addStatus($user_id, $time, $text,$mood);				
							
	        			}
	        			elseif (isset($_POST['btn_DeleteStatus'])){
		    			  		$statusId= $this->getRequest()->getParam("hdn_StatusCurrentDelete");
		    			  		$status->deleteStatus($statusId);		    			  	
		    			  }	
						
					}
    			}
    			$statuses = $status->getStatuses($this->_request->getQuery('id'));
    			$this->view->statuses = $statuses;
    			
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";  
        
        }        
    }

    public function basicAction()
    {	
    	try{    
    		$session = Zend_Registry::get('session');  		
    		
    		
	    	$basicInfo = new Application_Model_DbTable_Basicinfo();
	    	$flag_edit = 0; // dali da moze korisnikot da si ja editira stranicata
	    		
	    	if ($this->_request->getQuery('id') == ""){
	    		$basicInfo_user =$basicInfo->getBasicInfo($session->user['id']);
	    		$flag_edit = 1;	
	    	}
	    	else{
	    		$basicInfo_user =$basicInfo->getBasicInfo($this->_request->getQuery('id'));
	    		if ($basicInfo_user == null){
	    			//TODO da se napravi ako basicInfo bide prazano, nekakv msg ili da pravi redirect ili error page sto e najdobro
	    			$this->_helper->redirector('index','login');
	    		}    			
	    		
	    	}
	    
	    	$this->view->basicInfo_user = $basicInfo_user;
	    	$this->view->flag_edit = $flag_edit;
	    	$Query =$this->_request->getQuery('id');
	    	$this->view->Query = $Query;
    	}	
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }  		
    }

    public function medicalAction()
    {
    	try{    
    		$session = Zend_Registry::get('session');
    		
	    	$medicalInfo_BO = new Application_Model_DbTable_Medicalinfo();
	    	$bloodGroup_BO = new Application_Model_DbTable_Bloodgroup();	    	
	    	$medicalTreatment_BO = new Application_Model_DbTable_Medicalinfothreatmenthistory();
	    	$conditions_BO = new Application_Model_DbTable_Conditionmedicalinfo();
	    	
	    	$flag_edit = 0; // dali da moze korisnikot da si ja editira stranicata
	    		
	    	if ($this->_request->getQuery('id') == ""){
	    		$medicalInfo_user = $medicalInfo_BO->getMedicalInfo($session->user['id']);
	    		$flag_edit = 1;	
	    	}
	    	else{
	    		$medicalInfo_user = $medicalInfo_BO->getMedicalInfo($this->_request->getQuery('id'));
	    		
	    		if ($medicalInfo_user == null){
	    			//TODO da se napravi ako MedicalInfo bide prazano, nekakv msg ili da pravi redirect ili error page sto e najdobro
	    			$this->_helper->redirector('index','login');
	    		}  
	    	}	    	
	    	
	    	$bloodGroup = $bloodGroup_BO->getBloodgroupForUser($medicalInfo_user['BloodGroup_id']);	    	
	    	$currentThreatments = $medicalTreatment_BO->getCurrentThreatment($medicalInfo_user['id']);
	    	
	    	$flag_threatments = 1;
	    	
	    	if ($currentThreatments == null)
	    	{
	    		$flag_threatments = 0;	    	
	    	}
	    	
	    	$conditons = $conditions_BO->getConditionsForUser($medicalInfo_user['id']);
	    	
	    	$flag_conditions = 1;
	    	
    		if ($conditons == null)
	    	{
	    		$flag_conditions = 0;	    	
	    	}
	    	
	    	$Query = $this->_request->getQuery('id');
	    	
	    	$this->view->medicalInfo_user = $medicalInfo_user;
	    	$this->view->threatments = $currentThreatments;
	    	$this->view->conditons = $conditons;
	    	$this->view->flag_threatments = $flag_threatments;
	    	$this->view->flag_edit = $flag_edit;	
	    	$this->view->flag_conditions = $flag_conditions;	      	
	    	$this->view->bloodgroup = $bloodGroup;
	    	$this->view->Query = $Query;
	    	
    	}	
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }

    public function interestsAction()
    {
    	try{    
    		$session = Zend_Registry::get('session');
    		
	    	$interestsInfo_BO = new Application_Model_DbTable_Interestinfo();	    
	    	$interesttsInfoSports_BO   = new Application_Model_DbTable_Interestinfosport();
	    	
	    	$flag_edit = 0; // dali da moze korisnikot da si ja editira stranicata
	    		
	    	if ($this->_request->getQuery('id') == ""){
	    		$interestsInfo = $interestsInfo_BO->getInterestInfo($session->user['id']);
	    		$flag_edit = 1;	
	    	}
	    	else{
	    		$interestsInfo = $interestsInfo_BO->getInterestInfo($this->_request->getQuery('id'));
	    		
	    		if ($interestsInfo == null){
	    			//TODO da se napravi ako MedicalInfo bide prazano, nekakv msg ili da pravi redirect ili error page sto e najdobro
	    			$this->_helper->redirector('index','login');
	    		}  
	    	}	    	
	    	
	    	    	
	    	$sports = $interesttsInfoSports_BO->getSportsForUser($interestsInfo['id']);
	    	
	    	$flag_sports = 1;
	    	
	    	if ($sports == null)
	    	{
	    		$flag_sports = 0;	    	
	    	}
	    		   
	    	$Query = $this->_request->getQuery('id');
	    	
	    	$this->view->interestsInfo = $interestsInfo;
	    	$this->view->sports = $sports;	    	
	    	$this->view->flag_sports = $flag_sports;  
	    	$this->view->Query = $Query;
	    	
    	}	
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }

    public function basiceditAction()
    {
    	try{    
    		$session = Zend_Registry::get('session');
    		
	    	$basicInfo = new Application_Model_DbTable_Basicinfo();
	    	$flag_edit = 0; // dali da moze korisnikot da si ja editira stranicata
	    		
	    	if ($this->_request->getQuery('id') == ""){
	    		$basicInfo_user =$basicInfo->getBasicInfo($session->user['id']);
	    		$flag_edit = 1;	
	    	}
	    	else{
	    		
	    		
	    		if ($this->getRequest()->isPost()) {
	    				if (isset($_POST['btn_SaveBasic'])){
	    					
	    					//parametrite se zimaat vrz baza na name na kontrolata ne na id!!
	    					
	    					$FirstName = $this->getRequest()->getParam("txt_FirstName");
	    					$LastName = $this->getRequest()->getParam("txt_LastName");
	    					$City = $this->getRequest()->getParam("txt_City");
	    					$Country = $this->getRequest()->getParam("txt_Country");
	    					$About = $this->getRequest()->getParam("txt_About");
	    					$Birthday = $this->getRequest()->getParam("txt_Birthday");
	    					$Education = $this->getRequest()->getParam("txt_Education");
	    					$Gender =  $this->getRequest()->getParam("ddl_Gender");
	    					$permitions = $this->getRequest()->getParam("rbl_permitions");
	    					
	    					$Birthday_date =new Zend_Date($Birthday);
	    					$Birthday_date=$Birthday_date->toString('YYYY-MM-dd HH:mm:ss');
	    					//echo $Birthday_date;
	    					
	    					$basicInfo_user =$basicInfo->getBasicInfo($this->_request->getQuery('id'));
	    					$basicInfo->editBasicInfo($basicInfo_user['id'], $FirstName, $LastName, $Gender, $Birthday_date, $Education, $About, $Country, $City, $permitions);
	    					//echo "1111";
	    					//echo $basicInfo_user['id'];
	    					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
     						$redirector->gotoUrl('/Profile/basic?id='.$session->user['id']);
	    					
	    				}
	    				else{
	    					//TODO da se izhendla koga kje mu dojde vreme najverojatno nekoja badlogin page
	    					//nesto nepredvideno se ima sluiceno biejki se aktiviral POST request    	
	    					echo "asdasd0";	
	    				}	    				
	    		}
	    		$basicInfo_user =$basicInfo->getBasicInfo($this->_request->getQuery('id'));
		    	
		    	if ($basicInfo_user == null){
		    		//TODO da se napravi ako basicInfo bide prazano, nekakv msg ili da pravi redirect ili error page sto e najdobro
		    		$this->_helper->redirector('index','login');
		    	}  
	    	}
	    	$this->view->basicInfo_user = $basicInfo_user;
	    	//$this->view->flag_edit = $flag_edit;
	    	$Query =$this->_request->getQuery('id');
	    	$this->view->Query = $Query;
    	}	
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }  		
    
    }

    public function medicaleditAction()
    {
        try
        {
        	$session = Zend_Registry::get('session');
        	
        	$medicalInfo_BO = new Application_Model_DbTable_Medicalinfo();
        	$condition_BO = new Application_Model_DbTable_Conditions();
        	$conditionMedicalInfo_BO = new Application_Model_DbTable_Conditionmedicalinfo();
        	$threatment_BO = new Application_Model_DbTable_Threatmentshistory();
        	$threatmentMedicalInfo_BO = new Application_Model_DbTable_Medicalinfothreatmenthistory();
        	$bloodGroup_BO = new Application_Model_DbTable_Bloodgroup();
        	
        	$flag_edit = 0; // dali da moze korisnikot da si ja editira stranicata
	    		
	    	if ($this->_request->getQuery('id') == ""){
	    		$medicalInfo_user =$medicalInfo_BO->getMedicalInfo($session->user['id']);
	    		$flag_edit = 1;	
	    	}
	    	else{
	    		$medicalInfo_user =$medicalInfo_BO->getMedicalInfo($this->_request->getQuery('id'));
	    		
		    	if ($this->getRequest()->isPost()) {
		    				if (isset($_POST['btn_SaveEdit'])){
		    					
		    					//TODO validacija na floats
		    					$Weight =  $this->getRequest()->getParam("txt_Weight");
		    					$Height = $this->getRequest()->getParam("txt_Height");
		    					$bloodGroup = $this->getRequest()->getParam("ddl_Bloodgroup");		
		    					$permitions = $this->getRequest()->getParam("rbl_permitions");		    					
		    					
		    					$medicalInfo_user =$medicalInfo_BO->getMedicalInfo($this->_request->getQuery('id'));
		    					$medicalInfo_BO->editMedicalInfo($medicalInfo_user['id'],$Weight,$Height,$bloodGroup, $permitions);
		    					//$this->_helper->redirector('medical','Profile');
		    					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
     							$redirector->gotoUrl('/Profile/medical?id='.$session->user['id']);
			    			}
			    			elseif (isset($_POST['btn_ThreatmentSave'])){
			    				
			    				$threatment = $this->getRequest()->getParam("ddl_Threatment");
			    				//$dateFrom=$this->getRequest()->getParam("txt_ThreatmentDateFrom");	
			    				//$dateTo=$this->getRequest()->getParam("txt_ThreatmentDateTo");	
			    				
			    				$dateFrom =$this->getRequest()->getParam("txt_ThreatmentDateFrom");
			    				if ($dateFrom==""){
			    					echo "<script type='text/javascript'>alert('You must insert Date From!');</script>";
			    					$dateFrom ="NULL";
			    				}
			    				else{
			    					$dateFrom = new Zend_Date($dateFrom); 
			    					$dateFrom=$dateFrom->toString('YYYY-MM-dd HH:mm:ss');	
			    				}
			    				$dateTo = $this->getRequest()->getParam("txt_ThreatmentDateTo");
			    				if ($dateTo==""){
			    					echo "<script type='text/javascript'>alert('You must insert Date To!');</script>";
			    					$dateTo ="NULL";
			    				}
			    				else{
			    					$dateTo = new Zend_Date($dateTo);
			    					$dateTo=$dateTo->toString('YYYY-MM-dd HH:mm:ss');
			    				}
			    				
			    				
			    				if ($dateFrom > $dateTo){			    					
			    					echo "<script type='text/javascript'>alert('Date From must be before Date To!');</script>";
			    				}
			    				else {
			    					$threatmentMedicalInfo_BO->AddThreatmentForUser($medicalInfo_user['id'], $threatment, $dateFrom, $dateTo);
			    				}
			    				
		    				}
		    				elseif (isset($_POST['btn_CondtionSave'])) {
		    					
		    					$condtion = $this->getRequest()->getParam("ddl_Conditions");			    						    				
			    				$dateCondition =$this->getRequest()->getParam("txt_CondtitionDate");
			    				if ($dateCondition==""){
			    					echo "<script type='text/javascript'>alert('You must insert Date From!');</script>";
			    					$dateCondition ="NULL";
			    				}
			    				else{
			    					$dateCondition = new Zend_Date($dateCondition); 
			    					$dateCondition=$dateCondition->toString('YYYY-MM-dd HH:mm:ss');
			    					$conditionMedicalInfo_BO->AddCondtitionForUser($medicalInfo_user['id'], $condtion, $dateCondition);
			    				}
		    				
		    				}
		    			  elseif (isset($_POST['btn_DeleteCondition'])){
		    			  		$conditionDeleteId= $this->getRequest()->getParam("hdn_ConditionCurrentDelete");
		    			  		$conditionMedicalInfo_BO->deleteCondtionForUser($conditionDeleteId);
		    			  		//echo $conditionDeleteId;
		    			  }
		    			  elseif (isset($_POST['btn_DeleteThreatment'])){
		    			  		$threatmentDeleteId= $this->getRequest()->getParam("hdn_ThreatmentCurrentDelete");
		    			  		$threatmentMedicalInfo_BO->deleteThreatmentForUser($threatmentDeleteId);
		    			  	
		    			  }
		    			  
		    	}
		    	$medicalInfo_user =$medicalInfo_BO->getMedicalInfo($this->_request->getQuery('id'));
	    		
		    	if ($medicalInfo_user == null){
		    		//TODO da se napravi ako MedicalInfo bide prazano, nekakv msg ili da pravi redirect ili error page sto e najdobro
		    		$this->_helper->redirector('index','login');
		    	} 
		    	
	    	}
        	$bloodGroups = $bloodGroup_BO->getBloodGroups();	
	    	$currentThreatments = $threatmentMedicalInfo_BO->getCurrentThreatment($medicalInfo_user['id']);	    	
	    	$flag_threatments = 1;	    	
	    	if ($currentThreatments == null)
	    	{
	    		$flag_threatments = 0;	    	
	    	}
	    	
	    	$threatmentsNonActive=$threatmentMedicalInfo_BO->getAllThreatmentsNonActive($medicalInfo_user['id']);
        	$flag_threatmentsNonActive = 1;	    	
	    	if ($threatmentsNonActive == null)
	    	{
	    		$flag_threatmentsNonActive = 0;	    	
	    	}
	    	
	    	$condtionsNonActive=$conditionMedicalInfo_BO->getConditionsNonActiveForUser($medicalInfo_user['id']);
	    	$flag_conditionsNonActive = 1;	    	
    		if ($condtionsNonActive == null)
	    	{
	    		$flag_conditionsNonActive = 0;	    	
	    	}
	    	
	    	$conditons = $conditionMedicalInfo_BO->getConditionsForUser($medicalInfo_user['id']);
	    	$flag_conditions = 1;	    	
    		if ($conditons == null)
	    	{
	    		$flag_conditions = 0;	    	
	    	}
	    	
	    	
        	$this->view->medicalInfo_user = $medicalInfo_user;
	    	
	    	$this->view->threatments = $currentThreatments;
	    	$this->view->conditons = $conditons;
	    	$this->view->flag_threatments = $flag_threatments;
	    	$this->view->flag_edit = $flag_edit;	
	    	$this->view->flag_conditions = $flag_conditions;
	    	$this->view->bloodgroups = $bloodGroups;
	    	$this->view->threatmentsNonActive =$threatmentsNonActive;
        	$this->view->flag_threatmentsNonActive = $flag_threatmentsNonActive;
        	$this->view->condtionsNonActive = $condtionsNonActive;
	    	$this->view->flag_conditionsNonActive = $flag_conditionsNonActive;
	    	$Query =$this->_request->getQuery('id');
	    	$this->view->Query = $Query;
	    	
        }
        catch (Zend_Exception $e){
        	echo "Error message: " . $e->getMessage() . "\n";
        }
    }

    public function interestseditAction()
    {
    	try
        {
        	$session = Zend_Registry::get('session');
        	
        	$interests_BO = new Application_Model_DbTable_Interestinfo();
        	$interesttsInfoSports_BO = new Application_Model_DbTable_Interestinfosport();
        	
        	
        	$flag_edit = 0; // dali da moze korisnikot da si ja editira stranicata
        	if ($this->_request->getQuery('id') == ""){
	    		$interestsInfo =$interests_BO->getInterestInfo($session->user['id']);
	    		$flag_edit = 1;	
	    	}
       		else{
	    		$interestsInfo =$interests_BO->getInterestInfo($this->_request->getQuery('id'));
		    	if ($this->getRequest()->isPost()) {
		    				if (isset($_POST['btn_SaveEdit'])){
		    					
		    					//TODO validacija na floats
		    					$Work =  $this->getRequest()->getParam("txt_Work");
		    					$Music = $this->getRequest()->getParam("txt_Music");		    						
		    					$permitions = $this->getRequest()->getParam("rbl_permitions");		    					
		    					
		    					$interests_BO->editInterestsInfo($interestsInfo['id'],$Work,$Music,$permitions);
		    					//$this->_helper->redirector('interests','Profile');
		    					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
     							$redirector->gotoUrl('/Profile/interests?id='.$session->user['id']);
			    			}   
				    	   	if(isset($_POST['btn_SportSave'])){
				    	   	
				    	   		$sportSelected = $this->getRequest()->getParam("ddl_Sports");					    	   		
				    	   		$interesttsInfoSports_BO->addSport($interestsInfo['id'], $sportSelected);
				    	   	}
		    				elseif (isset($_POST['btn_DeleteSport'])){
		    			  		$SportDeleteId= $this->getRequest()->getParam("hdn_SportCurrentDelete");
		    			  		$interesttsInfoSports_BO->deleteSportForUser($SportDeleteId);
		    			  	
		    			  }				    	   
		    	}
		    	$interestsInfo =$interests_BO->getInterestInfo($this->_request->getQuery('id'));
		    	if ($interestsInfo == null){
		    		//TODO da se napravi ako MedicalInfo bide prazano, nekakv msg ili da pravi redirect ili error page sto e najdobro
		    		$this->_helper->redirector('index','login');
		    	} 
		    	
	    	}
	    	$sports = $interesttsInfoSports_BO->getSportsForUser($interestsInfo['id']);
	    	$sports_add = $interesttsInfoSports_BO->getSportsForUserNotAdded($interestsInfo['id']);
	    	
	    	
	    	$flag_sports = 1;
	    	
	    	if ($sports == null)
	    	{
	    		$flag_sports = 0;	    	
	    	}
	    	
        	$flag_sportsAdd = 1;
	    	
	    	if ($sports_add == null)
	    	{
	    		$flag_sportsAdd = 0;	    	
	    	}
	    		   
	    	$Query = $this->_request->getQuery('id');
	    	
	    	$this->view->interestsInfo = $interestsInfo;
	    	$this->view->sports = $sports;	    	
	    	$this->view->flag_sports = $flag_sports;  
	    	$this->view->Query = $Query;
	    	$this->view->Sports_Add= $sports_add;
	    	$this->view->flag_sportsAdd = $flag_sportsAdd; 
        }
        	
        catch (Zend_Exception $e){
        	echo "Error message: " . $e->getMessage() . "\n";
        }
    }


}













